package com.zatech.cgnci.project.controller;


import com.zatech.cgnci.project.dto.FileUploadDTO;
import com.zatech.cgnci.project.service.CgnciAccessoryService;
import com.zatech.cgnci.project.service.IMosFileService;
import com.zatech.cgnci.project.vo.CgnciAccessoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 附件 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/api/accessory")
@Api(produces = "application/json", tags = "附件api接口文档")
@RequiredArgsConstructor
public class CgnciAccessoryController {

    private final IMosFileService mosFileService;

    private final CgnciAccessoryService cgnciAccessoryService;

    @ApiOperation(value = "mos文件上传", notes = "mos文件上传")
    @PostMapping(value = "/mos/upload")
    public CgnciAccessoryVO uploadFileByMos(@Valid FileUploadDTO dto) throws IOException {
        return mosFileService.uploadFileByMos(dto);
    }

    @ApiOperation(value = "mos文件下载", notes = "mos文件下载")
    @GetMapping(value = "/mos/download", produces = "application/octet-stream")
    public String downFileByMos(@Valid @NotBlank(message = "fileId不能为空") Long accId) throws IOException {
        return mosFileService.downFileByMos(accId);
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "删除")
    public void deleteById(@RequestParam(value = "accId") Long accId) {
        cgnciAccessoryService.removeById(accId);
    }

    @GetMapping("/deleteByIds")
    @ApiOperation(value = "批量删除")
    public void deleteByIds(@RequestParam(value = "accIds") String accIds) {
        List<Long> accIdList = Arrays.stream(accIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        cgnciAccessoryService.removeByIds(accIdList);
    }


}

